import time
import feapder
import itertools

# 原始范围
original_range = range(1, 8)
# 额外的值
extra_values = [10000, 10001]

# 连接两个迭代器
combined_range = itertools.chain(original_range, extra_values)

class AirSpiderTest(feapder.AirSpider):
    def start_requests(self):
        url = "https://live.douyin.com/webcast/web/partition/detail/room/"
        for i in combined_range:
            for page in range(0,6):
                params = {
                    "aid": "6383",
                    "app_name": "douyin_web",
                    "live_id": "1",
                    "device_platform": "web",
                    "language": "zh-CN",
                    "enter_from": "link_share",
                    "cookie_enabled": "true",
                    "screen_width": "1920",
                    "screen_height": "1080",
                    "browser_language": "zh-CN",
                    "browser_platform": "Win32",
                    "browser_name": "Chrome",
                    "browser_version": "131.0.0.0",
                    "count":"15",
                    "offset":  str(page),
                    "partition": str(i),
                    "partition_type": "1",
                    "req_from": "2",
                }
                yield feapder.Request(url, params=params, method="GET")
                
        
        
    def download_midware(self, request):
        request.headers = {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "priority": "u=1, i",
            "referer": "https://live.douyin.com/category/1_2",
            "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
        }
        request.cookies = {
            "__ac_signature": "_02B4Z6wo00f01j4SRIgAAIDCCTaA1TZKclo-MkAAAOjY08",
        }
        return request
    
    def parse(self, request, response):
            item = {}
            room_id_list = []
            for i in response.json["data"]["data"]:
                if i["room"]["room_view_stats"]["display_value"] > 500:
                    room_id_list.append("https://live.douyin.com/"+i["web_rid"])
                item['urls'] = room_id_list
            if item == {}:
                return
            data = feapder.Request('http://127.0.0.1:25508/add_url/', json=item, method="POST").get_response()
            time.sleep(30)
            yield feapder.Request(request.url, params=request.params, method="GET",callback=self.parse)
            


        
if __name__ == "__main__":
    AirSpiderTest(4).start() 

        
